/*
    This file is part of SQLiteGTK, written by Aly Hirani

    SQLiteGTK is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    SQLiteGTK is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with SQLiteGTK.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "include.h"
#include "main_window.h"
#include "main_window_sqlite.h"

#define DATABASE_KEY "sqlite_database"

inline void
main_window_sqlite_set_database(GtkWindow *window, sqlite3 *db)
{
	g_assert(window != NULL);
	/* db can actually be null. This would be to close the previous database */
	g_object_set_data_full(G_OBJECT(window), DATABASE_KEY, db, (GDestroyNotify)sqlite3_close);
}

inline sqlite3*
main_window_sqlite_get_database(GtkWindow *window)
{
	g_assert(window != NULL);
	return g_object_get_data(G_OBJECT(window), DATABASE_KEY);
}

inline gboolean
main_window_sqlite_is_open(GtkWindow *window)
{
	g_assert(window != NULL);
	return main_window_sqlite_get_database(window) != NULL;
}

void
main_window_sqlite_open_database(GtkBuilder *builder, const gchar* filename)
{
	GObject *window;
	GtkStatusbar *statusbar;
	gint result, *statusbar_context_id, *statusbar_message_id;
	sqlite3 *db;
	
	g_assert(builder != NULL);
	g_assert(filename != NULL);
	
	window = gtk_builder_get_object(builder, "window");
	g_assert(window != NULL);
	
	statusbar = GTK_STATUSBAR(gtk_builder_get_object(builder, "statusbar"));
	g_assert(statusbar != NULL);
	
	statusbar_context_id = (gint*)g_object_get_data(window, DATABASE_STATUS_CONTEXT_KEY);
	statusbar_message_id = (gint*)g_object_get_data(window, DATABASE_STATUS_MESSAGE_KEY);
	
	if(statusbar_context_id && statusbar_message_id)
	{
		gtk_statusbar_remove(statusbar, *statusbar_context_id, *statusbar_message_id);
	}
	else
	{
		statusbar_context_id = g_malloc(sizeof(gint));
		g_assert(statusbar_context_id != NULL);
		g_object_set_data_full(window, DATABASE_STATUS_CONTEXT_KEY, statusbar_context_id, (GDestroyNotify)g_free);
		
		statusbar_message_id = g_malloc(sizeof(gint));
		g_assert(statusbar_message_id != NULL);
		g_object_set_data_full(window, DATABASE_STATUS_MESSAGE_KEY, statusbar_message_id, (GDestroyNotify)g_free);
	}
	
	*statusbar_context_id = gtk_statusbar_get_context_id(statusbar, DATABASE_STATUS_CONTEXT_KEY);
	
	result = sqlite3_open(filename, &db);
	
	g_object_set_data_full(window, DATABASE_KEY, db, (GDestroyNotify)sqlite3_close);
	
	if(result == SQLITE_OK)
	{
		*statusbar_message_id = gtk_statusbar_push(statusbar, *statusbar_context_id, "Database open");
		/* FIXME: This is broken */
		/* fill_master_notebook_page(builder); */
		main_window_controls_set_enabled(builder, TRUE);
	}
	else
	{
		*statusbar_message_id = gtk_statusbar_push(statusbar, *statusbar_context_id, "Database open failed");
		log_warning("Unable to open database, %s", filename);
		main_window_controls_set_enabled(builder, FALSE);
	}
}

gint
main_window_sqlite_execute_query_cb(void *data, gint argc, gchar **columns_data, gchar **columns_name)
{
	/*
	struct_sqlite_callback *struct_data;
	gint i;
	
	g_assert(data != NULL);
	struct_data = (struct_sqlite_callback*)data;
	
	if(!struct_data->columns_initialized)
	{
		GType *list_store_types = g_malloc(sizeof(GType)*argc);
		g_assert(list_store_types != NULL);
		
		for(i = 0 ; i < argc ; ++i)
		{
			gtk_tree_view_insert_column_with_attributes (struct_data->view, -1, columns_name[i], 
				gtk_cell_renderer_text_new(), "text", i, NULL);
			list_store_types[i] = G_TYPE_STRING;
		}
		struct_data->store = gtk_list_store_newv (argc, list_store_types);
		g_free(list_store_types);
		
		struct_data->columns_initialized = TRUE;
	}
	
	gtk_list_store_append (struct_data->store, &struct_data->iter);
	
	for(i = 0 ; i < argc ; ++i)
	{
		gtk_list_store_set (struct_data->store, &struct_data->iter, i, columns_data[i] ? columns_data[i] : "NULL", -1);
	}
	return 0;
	*/
	
	
	
	return 0;
}
